<?php

namespace app\admin\controller;

use think\facade\{View, Session, Db};
use app\BaseController;

class Banner extends Base
{
    // 首页轮播图设置
    public function bannerlst(){
        if ($this->Ajax) {
            $page = input("page", 1);
            $size = input("limit", 100);
            $mp = [];
 


            $lst = Db::name("banner")->where($mp)->order("xh asc")->page($page, $size)->select()->toArray();
            foreach ($lst as &$v) {
                $v['addtime'] = date("Y-m-d H:i:s", $v['addtime']);

                $v['c2_title']="不跳转";
                if($v['c1']==1){
                    $v['c2_title']="跳转类目：".Db::name("goods_classify")->where("id",$v['c2'])->value("title");
                }
                if($v['c1']==2){
                    $v['c2_title']="跳转商品：".Db::name("goods")->where("id",$v['c2'])->value("main_title");
                }
                if($v['c1']==3){
                    $v['c2_title']="跳转链接：".$v['c2'];
                }
         
            }
            $count = Db::name("banner")->where($mp)->count();



            return resmsg(0, "检索成功", $lst, $count);
        }
        return View::fetch();
    }
    // 开启或冻结轮播
    public function setBannerStatus()
    {
        $is = Db::name("banner")->where("id", input("id"))->update(['status' => input('status')]);
        return $is ? returnmsg(200, "操作成功") : returnmsg(199, "操作异常");
    }
    // 轮播添加或编辑
    public function banneredit()
    {
        $id = input('id');

        $msg = Db::name("banner")->find($id);
        
        if($msg){
            $msg['c2_title']="";
            if($msg['c1']==1){
                $msg['c2_title']=Db::name("goods_classify")->where("id",$msg['c2'])->value("title");
            }
            if($msg['c1']==2){
                $msg['c2_title']=Db::name("goods")->where("id",$msg['c2'])->value("main_title");
            }
        }else{
            $msg['c1']=0;
            $msg['c2']="";
            $msg['c2_title']="";
        }
       
        

        View::assign("msg", $msg);

        return View::fetch("banner/src/banneredit");
    }
    // 轮播提交
    public function bannerSave()
    {
        $param = input('param.', []);



        $file = [];
        if (request()->file()) {
            $file = request()->file('img');
        }

        $mp = [];
        $mp[] = $param['id'] ? ["id", "<>", $param['id']] : ["id", ">", 0];
        $param['title'] = trim($param['title']);
        $param['remark'] = trim($param['remark']);
        $is = Db::name("banner")->where($mp)->where("title", $param['title'])->value("id");

        if ($is) {
            return returnmsg(201, "轮播图标题不能重复");
        }

        if ($file) {
            $img = uniqid() . ".jpg";
            \think\facade\Filesystem::disk('files')->putFileAs("banner", $file, $img);
            $param['img'] = $img;
        }


        if (!$param['id']) {
            unset($param['id']);
            $param['addtime'] = time();
            $param['img'] = $param['img'] ?? _user_header_img_data;
            $param['xh'] = Db::name("banner")->count();
        }

        $is = Db::name("banner")->strict(false)->save($param);


        return $is ? returnmsg(200, "操作成功") : returnmsg(201, "操作失败");
    }
    // 轮播排序
    public function bannersortlst()
    {
        $lst = Db::name("banner")->order("xh asc")->select()->toArray();
        foreach ($lst as &$v) {
            $v['count'] = 99;
        }
        View::assign('lst', $lst);

        return View::fetch('banner/src/bannersortlst');
    }
    //轮播排序提交
    public function bannerSortSave()
    {
        $idarr = array_filter(explode(",", input('ids')));
        if ($idarr) {
            $arr = [];
            foreach ($idarr as $k => $v) {
                $arr[] = [
                    'id' => $v,
                    'xh' => $k
                ];
            }


            $el = new \app\admin\model\Banner();
            $res = $el->saveAll($arr);
        }

        return returnmsg(200, "数据更新成功");
    }
    // 删除轮播
    public function bannerDelete()
    {
        
        $is = Db::name("banner")->delete(input('id'));
        return returnmsg(200, "操作成功");
    }
    // 轮播选择商品
    public function bannergoods(){

        $lst=Db::name("goods")->field("id,main_title title,img")->order("id desc")->paginate(5)->each(function($item){
            $item['img']="/static/files/goodsruntime/".$item['img'];
            return $item;
        });

        // 自封组件-分帧选择器
        return $this->ComponentSrc("SelectHtml",[
            't1'=>'商品标题',
            't2'=>'商品',
            'lst'=>$lst
        ]);

    }
    // 轮播选择类目
    public function bannerclassify(){
        $lst=Db::name("goods_classify")->field("id,title,img")->order("xh asc")->paginate(5)->each(function($item){
            $item['img']="/static/files/classify/".$item['img'];
            return $item;
        });

        // 自封组件-分帧选择器
        return $this->ComponentSrc("SelectHtml",[
            't1'=>'类目信息',
            't2'=>'类目',
            'lst'=>$lst
        ]);
    }

}